#include <stdio.h>
#define PCRE2_STATIC 1  // 必须在包含pcre2.h之前定义
#define PCRE2_CODE_UNIT_WIDTH 8
#include "pcre2.h"

int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("Usage: %s <pattern> <subject>\n", argv[0]);
        return 1;
    }

    pcre2_code_8 *re;
    PCRE2_SPTR8 pattern = (PCRE2_SPTR8)argv[1];
    PCRE2_SPTR8 subject = (PCRE2_SPTR8)argv[2];
    int errornumber;
    PCRE2_SIZE erroroffset;

    // 编译正则表达式
    re = pcre2_compile_8(pattern, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
    if (re == NULL)
    {
        PCRE2_UCHAR8 buffer[256];
        pcre2_get_error_message_8(errornumber, buffer, sizeof(buffer));
        printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset, buffer);
        return 1;
    }

    // 执行匹配
    pcre2_match_data_8 *match_data = pcre2_match_data_create_from_pattern_8(re, NULL);
    int rc = pcre2_match_8(re, subject, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL);

    if (rc < 0)
    {
        printf("No match found\n");
    }
    else
    {
        printf("Match found at offset %d\n", (int)pcre2_get_ovector_pointer_8(match_data)[0]);
    }

    // 释放资源
    pcre2_match_data_free_8(match_data);
    pcre2_code_free_8(re);

    return 0;
}

#define PCRE2_STATIC 1  // 必须在包含pcre2.h之前定义
#define PCRE2_CODE_UNIT_WIDTH 8
#include "pcre2.h"